package com.xinghou.mq.consumer;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.*;
import com.aliyun.openservices.ons.api.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by qingshengzheng on 16/4/28.
 */
public class ConsumerUtil {
    private static Logger logger = LoggerFactory.getLogger(ConsumerUtil.class);
    public static void subscribe(String[] topis, Consumer consumer, final IConsumer iConsumer) {
        for (int i = 0; i < topis.length; i++) {
            consumer.subscribe(topis[i], "*", new MessageListener() {
                        @Override
                        public Action consume(Message message, ConsumeContext consumeContext) {
                            Long a = System.currentTimeMillis();
                            logger.info("------------------------------------------------------");
                            logger.info("key=[{}],body=[{}]",message.getKey(),new String(message.getBody()));
                            iConsumer.execute(message);
                            Long b = System.currentTimeMillis();
                            logger.info("rt=[---{}--- millsecond]",b-a);
                            logger.info("------------------------------------------------------");
                            return Action.CommitMessage;
                        }
                    }
            );

        }

    }


}
